/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidade.controller.JPA;

import entidade.controller.JPA.exceptions.NonexistentEntityException;
import entidade.controller.JPA.exceptions.RollbackFailureException;
import entidade.persistencia.FolhaPagamento;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import entidade.persistencia.Funcionario;
import entidade.persistencia.EntradaSaida;
import entidade.persistencia.OpercaoExtra;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.UserTransaction;

/**
 *
 * @author victorinox
 */
public class FolhaPagamentoJpaController {
    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "globalimage2PU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FolhaPagamento folhaPagamento) throws RollbackFailureException, Exception {
        if (folhaPagamento.getEntradaSaidaCollection() == null) {
            folhaPagamento.setEntradaSaidaCollection(new ArrayList<EntradaSaida>());
        }
        if (folhaPagamento.getOpercaoExtraCollection() == null) {
            folhaPagamento.setOpercaoExtraCollection(new ArrayList<OpercaoExtra>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Funcionario funcionarioId = folhaPagamento.getFuncionarioId();
            if (funcionarioId != null) {
                funcionarioId = em.getReference(funcionarioId.getClass(), funcionarioId.getFuncionarioId());
                folhaPagamento.setFuncionarioId(funcionarioId);
            }
            List<EntradaSaida> attachedEntradaSaidaCollection = new ArrayList<EntradaSaida>();
            for (EntradaSaida entradaSaidaCollectionEntradaSaidaToAttach : folhaPagamento.getEntradaSaidaCollection()) {
                entradaSaidaCollectionEntradaSaidaToAttach = em.getReference(entradaSaidaCollectionEntradaSaidaToAttach.getClass(), entradaSaidaCollectionEntradaSaidaToAttach.getEntradaSaidaId());
                attachedEntradaSaidaCollection.add(entradaSaidaCollectionEntradaSaidaToAttach);
            }
            folhaPagamento.setEntradaSaidaCollection(attachedEntradaSaidaCollection);
            List<OpercaoExtra> attachedOpercaoExtraCollection = new ArrayList<OpercaoExtra>();
            for (OpercaoExtra opercaoExtraCollectionOpercaoExtraToAttach : folhaPagamento.getOpercaoExtraCollection()) {
                opercaoExtraCollectionOpercaoExtraToAttach = em.getReference(opercaoExtraCollectionOpercaoExtraToAttach.getClass(), opercaoExtraCollectionOpercaoExtraToAttach.getOperacaoExtraId());
                attachedOpercaoExtraCollection.add(opercaoExtraCollectionOpercaoExtraToAttach);
            }
            folhaPagamento.setOpercaoExtraCollection(attachedOpercaoExtraCollection);
            em.persist(folhaPagamento);
            if (funcionarioId != null) {
                funcionarioId.getFolhaPagamentoCollection().add(folhaPagamento);
                funcionarioId = em.merge(funcionarioId);
            }
            for (EntradaSaida entradaSaidaCollectionEntradaSaida : folhaPagamento.getEntradaSaidaCollection()) {
                entradaSaidaCollectionEntradaSaida.getFolhaPagamentoCollection().add(folhaPagamento);
                entradaSaidaCollectionEntradaSaida = em.merge(entradaSaidaCollectionEntradaSaida);
            }
            for (OpercaoExtra opercaoExtraCollectionOpercaoExtra : folhaPagamento.getOpercaoExtraCollection()) {
                FolhaPagamento oldFolhaPagamentoIdOfOpercaoExtraCollectionOpercaoExtra = opercaoExtraCollectionOpercaoExtra.getFolhaPagamentoId();
                opercaoExtraCollectionOpercaoExtra.setFolhaPagamentoId(folhaPagamento);
                opercaoExtraCollectionOpercaoExtra = em.merge(opercaoExtraCollectionOpercaoExtra);
                if (oldFolhaPagamentoIdOfOpercaoExtraCollectionOpercaoExtra != null) {
                    oldFolhaPagamentoIdOfOpercaoExtraCollectionOpercaoExtra.getOpercaoExtraCollection().remove(opercaoExtraCollectionOpercaoExtra);
                    oldFolhaPagamentoIdOfOpercaoExtraCollectionOpercaoExtra = em.merge(oldFolhaPagamentoIdOfOpercaoExtraCollectionOpercaoExtra);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FolhaPagamento folhaPagamento) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            FolhaPagamento persistentFolhaPagamento = em.find(FolhaPagamento.class, folhaPagamento.getFolhaPagamentoId());
            Funcionario funcionarioIdOld = persistentFolhaPagamento.getFuncionarioId();
            Funcionario funcionarioIdNew = folhaPagamento.getFuncionarioId();
            List<EntradaSaida> entradaSaidaCollectionOld = persistentFolhaPagamento.getEntradaSaidaCollection();
            List<EntradaSaida> entradaSaidaCollectionNew = folhaPagamento.getEntradaSaidaCollection();
            List<OpercaoExtra> opercaoExtraCollectionOld = persistentFolhaPagamento.getOpercaoExtraCollection();
            List<OpercaoExtra> opercaoExtraCollectionNew = folhaPagamento.getOpercaoExtraCollection();
            if (funcionarioIdNew != null) {
                funcionarioIdNew = em.getReference(funcionarioIdNew.getClass(), funcionarioIdNew.getFuncionarioId());
                folhaPagamento.setFuncionarioId(funcionarioIdNew);
            }
            List<EntradaSaida> attachedEntradaSaidaCollectionNew = new ArrayList<EntradaSaida>();
            for (EntradaSaida entradaSaidaCollectionNewEntradaSaidaToAttach : entradaSaidaCollectionNew) {
                entradaSaidaCollectionNewEntradaSaidaToAttach = em.getReference(entradaSaidaCollectionNewEntradaSaidaToAttach.getClass(), entradaSaidaCollectionNewEntradaSaidaToAttach.getEntradaSaidaId());
                attachedEntradaSaidaCollectionNew.add(entradaSaidaCollectionNewEntradaSaidaToAttach);
            }
            entradaSaidaCollectionNew = attachedEntradaSaidaCollectionNew;
            folhaPagamento.setEntradaSaidaCollection(entradaSaidaCollectionNew);
            List<OpercaoExtra> attachedOpercaoExtraCollectionNew = new ArrayList<OpercaoExtra>();
            for (OpercaoExtra opercaoExtraCollectionNewOpercaoExtraToAttach : opercaoExtraCollectionNew) {
                opercaoExtraCollectionNewOpercaoExtraToAttach = em.getReference(opercaoExtraCollectionNewOpercaoExtraToAttach.getClass(), opercaoExtraCollectionNewOpercaoExtraToAttach.getOperacaoExtraId());
                attachedOpercaoExtraCollectionNew.add(opercaoExtraCollectionNewOpercaoExtraToAttach);
            }
            opercaoExtraCollectionNew = attachedOpercaoExtraCollectionNew;
            folhaPagamento.setOpercaoExtraCollection(opercaoExtraCollectionNew);
            folhaPagamento = em.merge(folhaPagamento);
            if (funcionarioIdOld != null && !funcionarioIdOld.equals(funcionarioIdNew)) {
                funcionarioIdOld.getFolhaPagamentoCollection().remove(folhaPagamento);
                funcionarioIdOld = em.merge(funcionarioIdOld);
            }
            if (funcionarioIdNew != null && !funcionarioIdNew.equals(funcionarioIdOld)) {
                funcionarioIdNew.getFolhaPagamentoCollection().add(folhaPagamento);
                funcionarioIdNew = em.merge(funcionarioIdNew);
            }
            for (EntradaSaida entradaSaidaCollectionOldEntradaSaida : entradaSaidaCollectionOld) {
                if (!entradaSaidaCollectionNew.contains(entradaSaidaCollectionOldEntradaSaida)) {
                    entradaSaidaCollectionOldEntradaSaida.getFolhaPagamentoCollection().remove(folhaPagamento);
                    entradaSaidaCollectionOldEntradaSaida = em.merge(entradaSaidaCollectionOldEntradaSaida);
                }
            }
            for (EntradaSaida entradaSaidaCollectionNewEntradaSaida : entradaSaidaCollectionNew) {
                if (!entradaSaidaCollectionOld.contains(entradaSaidaCollectionNewEntradaSaida)) {
                    entradaSaidaCollectionNewEntradaSaida.getFolhaPagamentoCollection().add(folhaPagamento);
                    entradaSaidaCollectionNewEntradaSaida = em.merge(entradaSaidaCollectionNewEntradaSaida);
                }
            }
            for (OpercaoExtra opercaoExtraCollectionOldOpercaoExtra : opercaoExtraCollectionOld) {
                if (!opercaoExtraCollectionNew.contains(opercaoExtraCollectionOldOpercaoExtra)) {
                    opercaoExtraCollectionOldOpercaoExtra.setFolhaPagamentoId(null);
                    opercaoExtraCollectionOldOpercaoExtra = em.merge(opercaoExtraCollectionOldOpercaoExtra);
                }
            }
            for (OpercaoExtra opercaoExtraCollectionNewOpercaoExtra : opercaoExtraCollectionNew) {
                if (!opercaoExtraCollectionOld.contains(opercaoExtraCollectionNewOpercaoExtra)) {
                    FolhaPagamento oldFolhaPagamentoIdOfOpercaoExtraCollectionNewOpercaoExtra = opercaoExtraCollectionNewOpercaoExtra.getFolhaPagamentoId();
                    opercaoExtraCollectionNewOpercaoExtra.setFolhaPagamentoId(folhaPagamento);
                    opercaoExtraCollectionNewOpercaoExtra = em.merge(opercaoExtraCollectionNewOpercaoExtra);
                    if (oldFolhaPagamentoIdOfOpercaoExtraCollectionNewOpercaoExtra != null && !oldFolhaPagamentoIdOfOpercaoExtraCollectionNewOpercaoExtra.equals(folhaPagamento)) {
                        oldFolhaPagamentoIdOfOpercaoExtraCollectionNewOpercaoExtra.getOpercaoExtraCollection().remove(opercaoExtraCollectionNewOpercaoExtra);
                        oldFolhaPagamentoIdOfOpercaoExtraCollectionNewOpercaoExtra = em.merge(oldFolhaPagamentoIdOfOpercaoExtraCollectionNewOpercaoExtra);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = folhaPagamento.getFolhaPagamentoId();
                if (findFolhaPagamento(id) == null) {
                    throw new NonexistentEntityException("The folhaPagamento with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            FolhaPagamento folhaPagamento;
            try {
                folhaPagamento = em.getReference(FolhaPagamento.class, id);
                folhaPagamento.getFolhaPagamentoId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The folhaPagamento with id " + id + " no longer exists.", enfe);
            }
            Funcionario funcionarioId = folhaPagamento.getFuncionarioId();
            if (funcionarioId != null) {
                funcionarioId.getFolhaPagamentoCollection().remove(folhaPagamento);
                funcionarioId = em.merge(funcionarioId);
            }
            List<EntradaSaida> entradaSaidaCollection = folhaPagamento.getEntradaSaidaCollection();
            for (EntradaSaida entradaSaidaCollectionEntradaSaida : entradaSaidaCollection) {
                entradaSaidaCollectionEntradaSaida.getFolhaPagamentoCollection().remove(folhaPagamento);
                entradaSaidaCollectionEntradaSaida = em.merge(entradaSaidaCollectionEntradaSaida);
            }
            List<OpercaoExtra> opercaoExtraCollection = folhaPagamento.getOpercaoExtraCollection();
            for (OpercaoExtra opercaoExtraCollectionOpercaoExtra : opercaoExtraCollection) {
                opercaoExtraCollectionOpercaoExtra.setFolhaPagamentoId(null);
                opercaoExtraCollectionOpercaoExtra = em.merge(opercaoExtraCollectionOpercaoExtra);
            }
            em.remove(folhaPagamento);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FolhaPagamento> findFolhaPagamentoEntities() {
        return findFolhaPagamentoEntities(true, -1, -1);
    }

    public List<FolhaPagamento> findFolhaPagamentoEntities(int maxResults, int firstResult) {
        return findFolhaPagamentoEntities(false, maxResults, firstResult);
    }

    private List<FolhaPagamento> findFolhaPagamentoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from FolhaPagamento as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FolhaPagamento findFolhaPagamento(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FolhaPagamento.class, id);
        } finally {
            em.close();
        }
    }

    public int getFolhaPagamentoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from FolhaPagamento as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
